
from collections import defaultdict

def element_count(str_obj):
    d = defaultdict(int)
    for element in str_obj:
        d[element] += 1
    print(d.items())
    print(sorted( d.items(), key=lambda x: x[1], reverse=True)[:3])

#不用字典，sorted 自己实现
#
def _sorted(str_obj):
    s = set(str_obj)
    l = [ ]
    for element in s:
        l.append((element,str_obj.count(element)))
    print(l)
    dst_sort =[]
    for x in l:
        for index,y in enumerate(dst_sort):
            if x[1] > y[1]:
                dst_sort.insert(index,x)
                break
        else:
            dst_sort.append(x)
    print(dst_sort[:3])

_sorted("abcavaefegsgdghfdefavbdr")

# 写的很好


